package logic

import (
	"context"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"

	"month/rpc/internal/svc"
	"month/rpc/month"

	"github.com/zeromicro/go-zero/core/logx"
)

type PingLunLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPingLunLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLunLogic {
	return &PingLunLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论
func (l *PingLunLogic) PingLun(in *month.PingLunRequest) (*month.EsResponse, error) {
	// todo: add your logic here and delete this line

	gid := in.Gid
	uid := in.Uid
	text := in.Text

	db, err := gorm.Open(mysql.Open("root:q025l6263@tcp(43.143.123.142:3306)/2107a"), &gorm.Config{})
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库连接失败",
		}, nil
	}

	err = db.Exec("INSERT INTO `comment` (`gid`,`uid`,`text`) values (?,?,?)", gid, uid, text).Error
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "添加评论失败",
		}, nil
	}

	return &month.EsResponse{
		Code:    200,
		Message: "新增数据成功",
	}, nil
}
